package com.psd.demo.common;

import javax.servlet.http.HttpServletRequest;

public class Page {
	private int totalRows;//总行数
	private int pageSize=2;//每页显示数目
	private int totalPages;//总页数
	private int currentPage;//当前页数
	private int startRow;//当前数据库中起始行
	
	
	
	
	public Page(int _totalRows) {
		totalRows=_totalRows;
		totalPages=totalRows/pageSize;
		int mod = totalRows%pageSize;
		if(mod >0){
			totalPages++;
			currentPage=0;
			startRow=0;
		}
	}
	
	
	
	public int getTotalRows() {
		return totalRows;
	}
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalPages() {
		return totalPages;
	}
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getStartRow() {
		return startRow;
	}
	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}
	/**
	 * 首页
	 */
	public void first(){
		currentPage=1;
		startRow=1;
	}
	/**
	 * 前一页
	 */
	public void privious(){
		if(currentPage==1){
			return;
		}
		currentPage--;
		startRow=(currentPage-1)*pageSize;
	}
	/**
	 * 下一页
	 */
	public void next(){
		if(currentPage<totalPages){
			currentPage++;
		}
		startRow=(currentPage-1)*pageSize;
	}
	/**
	 * 尾页
	 */
	public void last(){
		currentPage=totalPages;
		startRow=(currentPage-1)*pageSize;
	}
	
	public boolean hasFirstPage(){
		if(getCurrentPage()>1){
			return true;
		}else return false;
	}
	public boolean hasLastPage(){
		if((getCurrentPage()<getTotalPages())&&(getTotalPages()!=1)){
			 return true;
	      }
	    else return false;
	}
	  /**
     *   是否有下一页 
     *   @return 是否有下一页 
     */   
 public boolean hasNextPage() {
     if (getTotalRows()>pageSize) {
       // currentPage++;
     //   startRow = (currentPage - 1) * pageSize;
        
         return (this.getCurrentPage()<this.getTotalPages());
       }
       else   return false;


   }

	/**
	 * 是否有上一页
	 * @return
	 */
	public boolean hasPreviousPage(){
		return (this.getCurrentPage()>1);
	}
	 public String getHTML(HttpServletRequest request){
         StringBuffer html = new StringBuffer();

     if (hasFirstPage()){
         html.append("<a href=\"")
                 .append(request.getRequestURI())
                 .append("?pageNo=1\">首页")
                 .append("</a>");

         }else
         {
         html.append("   ")
             .append("首页");
         }
         
     if (hasPreviousPage()){
         html.append("   <a href=\"")
         .append(request.getRequestURI())
         .append("?pageNo=")
         .append(getCurrentPage()-1)
         .append("\">上页")
         .append("</a>");
         } else{
       html.append("   ")
           .append("上页");
     }
                  
     if (hasNextPage()){
         html.append("   <a href=\"")
         .append(request.getRequestURI())
         .append("?pageNo=")
         .append(getCurrentPage()+1)
         .append("\">下页")
         .append("</a>");
         } else{
       html.append("   ")
           .append("下页");
       }

     if (hasLastPage()){
         html.append("   <a href=\"")
         .append(request.getRequestURI())
         .append("?pageNo=")
         .append(getTotalPages())
         .append("\">末页")
         .append("</a>   ");
         } else{
         html.append("   ")
           .append("末页");
       }
         
         html.append("【第").append(getCurrentPage()).append("页】");         
         html.append("【共").append(getTotalPages()).append("页】");         
         html.append("【共").append(getTotalRows()).append("条】"); 
         return html.toString();     
         }

}
